package cn.sll.org.controller;

import cn.sll.base.jwt.UserInfo;
import cn.sll.base.util.AjaxResult;
import cn.sll.base.util.LoginContext;
import cn.sll.base.util.PageList;
import cn.sll.org.domain.Employee;
import cn.sll.org.query.EmployeeQuery;
import cn.sll.org.service.impl.EmployeeServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description = "员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private EmployeeServiceImpl employeeService;

    @GetMapping
    public List<Employee> list(){
        return employeeService.findAll();
    }

    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){

            if (employee.getId()!=null){
                employeeService.update(employee);
            }else {
                employeeService.add(employee);
            }

        return  AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        employeeService.delete(id);
        return  AjaxResult.me().success();
    }

    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
//        UserInfo userInfo = LoginContext.getUserInfo();
//        if (null != userInfo.getShop()){
//            query.setShopId(userInfo.getShop().getId());
//        }
        return employeeService.queryByPage(query);
    }

    @PatchMapping()
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        employeeService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    @GetMapping("/findByDeptId/{deptId}")
    public List<Employee> findByDeptId(@PathVariable("deptId") Long deptId){
        return employeeService.findByDeptId(deptId);
    }
}
